package com.kxmall.web.controller.rider;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.rider.domain.bo.KxRiderBo;
import com.kxmall.rider.domain.vo.KxRiderVo;
import com.kxmall.web.controller.rider.service.IKxRiderService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 配送
 *
 * @author kxmall
 * @date 2023-09-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/rider/rider")
public class KxRiderController extends BaseController {

    private final IKxRiderService iKxRiderService;

    /**
     * 查询配送列表
     */
    @GetMapping("/getRiderByStorageId")
    public R<List<KxRiderVo>> getRiderByStorageId(Long storageId) {
        return R.ok(iKxRiderService.getRiderByStorageId(storageId));
    }


    /**
     * 查询配送列表
     */
    @SaCheckPermission("rider:rider:list")
    @GetMapping("/list")
    public TableDataInfo<KxRiderVo> list(KxRiderBo bo, PageQuery pageQuery) {
        bo.setStorageIds(getLoginUser().getStoragePermission());
        return iKxRiderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出配送列表
     */
    @SaCheckPermission("rider:rider:export")
    @Log(title = "配送", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KxRiderBo bo, HttpServletResponse response) {
        List<KxRiderVo> list = iKxRiderService.queryList(bo);
        ExcelUtil.exportExcel(list, "配送", KxRiderVo.class, response);
    }

    /**
     * 获取配送详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("rider:rider:query")
    @GetMapping("/{id}")
    public R<KxRiderVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long id) {
        return R.ok(iKxRiderService.queryById(id));
    }

    /**
     * 新增配送
     */
    @SaCheckPermission("rider:rider:add")
    @Log(title = "配送", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KxRiderBo bo) {
        String username = getLoginUser().getUsername();
        bo.setUserName(username);
        return toAjax(iKxRiderService.insertByBo(bo));
    }

    /**
     * 修改配送
     */
    @SaCheckPermission("rider:rider:edit")
    @Log(title = "配送", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KxRiderBo bo) {
        return toAjax(iKxRiderService.updateByBo(bo));
    }

    /**
     * 删除配送
     *
     * @param ids 主键串
     */
    @SaCheckPermission("rider:rider:remove")
    @Log(title = "配送", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iKxRiderService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 配送员工作状态批量更新为休息中
     */
    @PostMapping("/updateBusinessStateToRest")
    public R<String> updateBusinessStateToRest(@RequestBody String ids) {
        return R.ok(iKxRiderService.updateBusinessStateToRest(ids));
    }

    /**
     * 配送员工作状态批量更新为开工中
     */
    @PostMapping("/updateBusinessWorkState")
    public R<String> updateBusinessWorkState(@RequestBody String ids) {
        return R.ok(iKxRiderService.updateBusinessWorkState(ids));
    }

    /**
     * 配送员状态批量更新为禁用
     */
    @PostMapping("/updateStateToAbort")
    public R<String> updateStateToAbort(@RequestBody String ids) {
        return R.ok(iKxRiderService.updateStateToAbort(ids));
    }

    /**
     * 配送员状态批量更新为正常
     */
    @PostMapping("/updateStateToNomral")
    public R<String> updateStateToNomral(@RequestBody String ids) {
        return R.ok(iKxRiderService.updateStateToNomral(ids));
    }


    /**
     * 获取指定骑手的推送订阅二维码
     */
    @GetMapping("/getRiderQrcodeImage")
    public R<String> getRiderQrcodeImage(Long riderId){
        return R.ok("操作成功",iKxRiderService.getRiderQrcodeImage(riderId));
    }

}
